/**
 * 
 */
package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.request.domain.Request;
import org.ediba.siri.core.request.domain.RequestModel;
import org.ediba.siri.core.request.service.RequestService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.ResponseBody;

public class RequestModelController extends Controller {

	private RequestService requestService;
	public void setRequestService(RequestService requestService) {this.requestService = requestService;}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, RequestModelCommand command){
		List<RequestModel> requestModels = null;
		Integer total = 0;
		
		if (command.getIdRequest()!=null){
			Request req = this.requestService.retrieve(command.getIdRequest());
			requestModels = new ArrayList<RequestModel>(req.getRequestmodels());
			total = requestModels.size();
		}
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", requestModels);
		_result.put("total", total);
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}

	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, RequestModelCommand command){
		
		RequestModel requestModel = null;
		
		Boolean gender = null;
		Integer ageMin = null;
		Integer ageMax = null;
		Integer count = null;
		String costume = null;
		String color = null;
		String others = null;
		
		try{
			if (command.getData().getInt("gender")==1)
				gender = true;
			else if (command.getData().getInt("gender")==0)
				gender = false;
		} catch (JSONException e) {e.printStackTrace();}
		try{ageMin = command.getData().getInt("ageMin");
		} catch (JSONException e) {e.printStackTrace();}
		try{ageMax = command.getData().getInt("ageMax");
		} catch (JSONException e) {e.printStackTrace();}
		try{count = command.getData().getInt("count");
		} catch (JSONException e) {e.printStackTrace();}
		try{costume = command.getData().getString("costume");
		} catch (JSONException e) {e.printStackTrace();}
		try{color = command.getData().getString("color");
		} catch (JSONException e) {e.printStackTrace();}
		try{others = command.getData().getString("others");
		} catch (JSONException e) {e.printStackTrace();}
		
		Integer idRequest = null;
		idRequest = command.getIdRequest();
		
		requestModel = this.requestService.modelCreate(idRequest);
		requestModel.setGender(gender);
		requestModel.setAgeMin(ageMin);
		requestModel.setAgeMax(ageMax);
		requestModel.setCount(count);
		requestModel.setCostume(costume);
		requestModel.setColor(color);
		requestModel.setOthers(others);
		requestModel = this.requestService.modelUpdate(requestModel);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", requestModel);
		_result.put("success", requestModel!=null);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, RequestModelCommand command){
		Integer id = null;
		try {
			id = command.getData().getInt("id");
		} catch (JSONException e) {e.printStackTrace();}
		
		boolean _success = this.requestService.modelDelete(id);
		
		Map<String,Object> _result = new HashMap<String,Object>(1);
		_result.put("success", _success);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, RequestModelCommand command){
		
		Integer id = null;
		Boolean gender = null;
		Integer ageMin = null;
		Integer ageMax = null;
		Integer count = null;
		String costume = null;
		String color = null;
		String others = null;
		RequestModel requestModel;
		requestModel = this.requestService.modelRetrieve(id);
		try {
			id = command.getData().getInt("id");
			try{
				if (command.getData().getInt("gender")==1)
					gender = true;
				else if (command.getData().getInt("gender")==0)
					gender = false;
				requestModel.setGender(gender);
			} catch (JSONException e) {e.printStackTrace();}
			try{
				ageMin = command.getData().getInt("ageMin");
				requestModel.setAgeMin(ageMin);
			} catch (JSONException e) {e.printStackTrace();}
			try{
				ageMax = command.getData().getInt("ageMax");
				requestModel.setAgeMax(ageMax);
			} catch (JSONException e) {e.printStackTrace();}
			try{
				count = command.getData().getInt("count");
				requestModel.setCount(count);
			} catch (JSONException e) {e.printStackTrace();}
			try{
				costume = command.getData().getString("costume");
				requestModel.setCostume(costume);
			} catch (JSONException e) {e.printStackTrace();}
			try{
				color = command.getData().getString("color");
				requestModel.setColor(color);
			} catch (JSONException e) {e.printStackTrace();}
			try{
				others = command.getData().getString("others");
				requestModel.setOthers(others);
			} catch (JSONException e) {e.printStackTrace();}
		} catch (JSONException e) {e.printStackTrace();}
		
		requestModel = this.requestService.modelUpdate(requestModel);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", requestModel);
		_result.put("success", requestModel!=null);
		return _result;
	}
	
}
